package a_arrays;

import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

//Arrays的常用方法
public class ArraysDemo01 {
    public static void main(String[] args) {
        //1.返回数组中的内容
        int[] arr = {10,20,30,40,50,60};
        //toString返回值是String,所以这个方法只能做查看,不能操作元素
        System.out.println(Arrays.toString(arr));//[10, 20, 30, 40, 50, 60]

        //2.拷贝数组(指定范围,范围: 前包含后不包含)
        int[] range = Arrays.copyOfRange(arr, 1, 4);
        System.out.println(Arrays.toString(range));//[20, 30, 40]

        //3.拷贝数组,可以指定新数组的长度
        int[] ints = Arrays.copyOf(arr, 10);
        System.out.println(Arrays.toString(ints));//[10, 20, 30, 40, 50, 60, 0, 0, 0, 0]

        //4.把数组中的原数据修改为新数据后又存入到数组中
        double[] prices = {99.9,128,100};
        //把所有的价格打八折,然后又继续存入数组中
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                //value = 0 1 2
                return prices[value] * 0.8;
            }
        });
        System.out.println(Arrays.toString(prices));//[79.92000000000002, 102.4, 80.0]


        //5.对数组进行排序(默认是升序排序)
        Arrays.sort(prices);
        System.out.println(Arrays.toString(prices));//[79.92000000000002, 80.0, 102.4]
    }
}














